﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using HollywoodHackers.Storage.Models;
using HollywoodHackers.Storage.Repositories;

namespace HollywoodHackers.Web.Controllers
{
    public class UserTextNotificationController : Controller
    {
        UserTextNotificationRepository repository;

        public UserTextNotificationController()
        {
            repository = new UserTextNotificationRepository();
        }

        //
        // GET: /UserTextNotification/

        public ActionResult Index()
        {
            return View();
        }

        public JsonResult GetMessages()
        {
            if (User.Identity.IsAuthenticated)
            {
                UserTextNotification[] userToasts = repository.GetNotificationsForUser(User.Identity.Name);
                object[] data = (from UserTextNotification toast in userToasts
                                 select new { title = toast.Title ?? "Notification", text = toast.MessageText }).ToArray();

                return Json(data, JsonRequestBehavior.AllowGet);
            }
            else
                return Json(null);
        }

    }
}